﻿var lastTaskElem = null;
var lastTaskElemBg = null;
var taskOperation = "";
var taskSessionId = null;

function selectTaskTableElement(elemID) {
    if (lastTaskElem != null) {
        document.getElementById(lastTaskElem).style.backgroundColor = lastTaskElemBg;
    }
    var elem = document.getElementById(elemID);
    lastTaskElem = elemID;
    lastTaskElemBg = elem.style.backgroundColor;
    elem.style.backgroundColor = "#FEF0C9";
}


function showTaskOperation(operation) 
{
    if (operation == "Add") {
        document.getElementById('btnTaskOperation').value = "Add Task";
        document.getElementById('tdOperation').innerHTML = "Add Task";
        clearTaskPopUpElements();
    }
    else if (operation == "Edit") {
        if (lastTaskElem == null) {
            LoadItemAttatchments();
            alert('No Task Selected');
        }
        loadLastTaskElements();
        document.getElementById('btnTaskOperation').value = "Save Changes";
        document.getElementById('tdOperation').innerHTML = "Edit Task";
    }
    document.getElementById('divTaskPopup').style.display = '';
    RelocateTaskPopup();
    document.getElementById('divTasks').style.opacity = 0.5;
    linkOperation = operation;

}

function LoadItemAttatchments() {
}

function clearTaskPopUpElements() {
    document.getElementById('txtTaskDescription').value = '';
}

function RelocateTaskPopup() {
    var popup = document.getElementById("divTaskPopup")
    if (popup.style.display != 'none') {
        var width = getViewPortWidth();
        width -= 800;
        width -= popup.style.width.replace("px", "");
        popup.style.left = width / 2 + 'px';
        popup.style.top = "50px";
    }
}

function CancelTaskOperation() {
    //clear tasksession
    document.getElementById('divTaskPopup').style.display = 'none';
    document.getElementById('divTasks').style.opacity = 1.0;
    toggleLinkButtons(true);
}

function toggleLinkButtons(enable) {
    document.getElementById('btnAddTask').disabled = !enable;
    document.getElementById('btnEdiTask').disabled = !enable;
    document.getElementById('btnDelete').disabled = !enable;
}

function loadLastTaskElements() {
    var elem = document.getElementById(lastTaskElem);
    var elemChild = elem.getElementsByTagName("td");
    document.getElementById('txtTaskDescription').value = elemChild[1].innerHTML;

}


function openTaskAttatchmentUpload() {

    //document.getElementById('fileFakeAttatchment').disabled = true;
    
    var uploader = document.getElementById('frmUploader');
    var browseButton = uploader.contentDocument.getElementById('txtFileUploader');
    var returnDiv = uploader.contentDocument.getElementById('txtReturnDiv')
    var ddUploadOperation = uploader.contentDocument.getElementById('ddUploadOperation')
    var txtTaskSession = uploader.contentDocument.getElementById('txtTaskSession')

    if (taskSessionId == null) {
        taskSessionId = 'task_temp_' + uniqid();
    }

    txtTaskSession.value = taskSessionId;
    returnDiv.value = "divFileAttatchments";
    ddUploadOperation.value = 'Task';
    browseButton.click();
}


function PerformTaskOperation() {

    taskDescription = document.getElementById('txtTaskDescription').value;
    txtTaskDueDate = document.getElementById('txtTaskDueDate').value;
    txtTaskDetails = document.getElementById('txtTaskDetails').value;

    errMsg = "";
    
    if (taskDescription == "") {
        errMsg += "Description is Required<br/>";
    }

    if (txtTaskDueDate == "") {
        errMsg += "Due Date is Required<br/>";
    }

    if (txtTaskDetails == "") {
        errMsg += "Task Details is Required<br/>";
    }

    document.getElementById('lblErrorMessage').innerHTML = errMsg;

    if (errMsg != "") {
        return;
    }


    var url = "Personal/Tasks.aspx";

    if (linkOperation == "Add") {
        url = url + "?addtask=1";
    }
    else if (linkOperation == "Edit") {
        url = url + "?edittask=1";
        try {
            taskNo = document.getElementById(lastTaskElem).getAttribute("taskNo");
        } catch (ex) {
            alert('No Task Selected');
            return;
        }
        url = url + "&taskNo=" + URLEncode(linkNo);
    }


    url = url + "&taskDescription=" + URLEncode(taskDescription);
    url = url + "&taskDueDate=" + URLEncode(txtTaskDueDate);
    url = url + "&taskDetails=" + URLEncode(txtTaskDetails);
    url = url + "&taskSessionId=" + taskSessionId;
    url = url + "&sid=" + Math.random();

    //alert(url);
    response = SubmitLinkForm(url);
    //alert(response);
    if (response.indexOf("Success") > -1) {
          loadXMLDoc('Personal/Tasks.aspx');
          if (lastTaskElem != null) {
              document.getElementById(lastTaskElem).style.backgroundColor = "#FEF0C9";
          }

          if (taskOperation == "Add") {
              alert("New Link Successfully Added");
              taskSessionId = null;
              document.getElementById('divFileAttatchments').innerHTML = ''; 
            }
            else if (taskOperation == "Edit") {
                alert("Link Successfully Updated");
            }
    } else {
        document.getElementById('lblErrorMessage').innerHTML = response;
    }

    return false;
}



function SubmitTaskForm(url) {
    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    }
    else {// code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.open("GET", url, false);
    xmlhttp.send(null);
    response = xmlhttp.responseText;
    response = response.substring(response.indexOf("<response>"), response.indexOf("</response"));
    return response;
}